/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-10-12 17:32:18
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.db.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.DateUtils;
import cn.cmcc.online.smsapi.sdk.util.DuoquUtils;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;

public class PopupActionManager {

    public static final String SCENE_ID = "scene_id";// 情景号

    public static final String DATE = "date";// 日期

    public static final String PARSE_TIMES = "parse_times";// 识别次数

    public static final String POPUP_TIMES = "popup_times";// 展现次数

    public static final String TABLE_NAME = "tb_popup_action_scene";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS "
            + TABLE_NAME;

    public static final String CREATE_TABLE = "create table  if not exists "
            + TABLE_NAME + " ("

            + SCENE_ID + " TEXT, " + DATE + " TEXT, " + PARSE_TIMES
            + " INTEGER DEFAULT '0', " + POPUP_TIMES + " INTEGER DEFAULT '0'"
            + " ) ";

    public static List<PopupActionInfo> queryDates(String updateDate) {
        XyCursor cur = null;
        List<PopupActionInfo> list = new ArrayList<PopupActionInfo>();
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { DATE }, DATE
                    + " < ? ", new String[] { updateDate }, DATE, null, null,
                    null);
            if (cur != null && cur.getCount() > 0) {
                int dateIndex = cur.getColumnIndex(DATE);

                PopupActionInfo info = null;
                while (cur.moveToNext()) {
                    info = new PopupActionInfo();
                    info.date = cur.getString(dateIndex);
                    list.add(info);
                }
                return list;
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "queryDates: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return list;
    }

    public static List<PopupActionInfo> queryByDate(String date) {
        List<PopupActionInfo> infoList = new ArrayList<PopupActionInfo>();
        XyCursor cur = null;
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { SCENE_ID, DATE,
                    PARSE_TIMES, POPUP_TIMES }, DATE + " = ? ",
                    new String[] { date }, null, null, null, null);
            if (cur != null && cur.getCount() > 0) {
                int sceneIdIndex = cur.getColumnIndex(SCENE_ID);
                int dateIndex = cur.getColumnIndex(DATE);
                int parseTimesIndex = cur.getColumnIndex(PARSE_TIMES);
                int popupTimesIndex = cur.getColumnIndex(POPUP_TIMES);
                PopupActionInfo info = null;
                while (cur.moveToNext()) {
                    info = new PopupActionInfo();
                    info.sceneId = cur.getString(sceneIdIndex);
                    info.date = cur.getString(dateIndex);
                    info.parseTimes = cur.getInt(parseTimesIndex);
                    info.popupTimes = cur.getInt(popupTimesIndex);
                    infoList.add(info);
                }

            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "queryByDate: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return infoList;
    }

    public static PopupActionInfo queryInfoBySceneId(String scene_id) {
        XyCursor cur = null;
        PopupActionInfo info = null;
        String date = DateUtils.getCurrentTimeString("yyyyMMdd");
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { SCENE_ID, DATE,
                    PARSE_TIMES, POPUP_TIMES }, SCENE_ID + " = ? and " + DATE
                    + " = ? ", new String[] { scene_id, date }, null, null,
                    null, "1");
            if (cur != null && cur.getCount() > 0) {
                int sceneIdIndex = cur.getColumnIndex(SCENE_ID);
                int dateIndex = cur.getColumnIndex(DATE);
                int parseTimesIndex = cur.getColumnIndex(PARSE_TIMES);
                int popupTimesIndex = cur.getColumnIndex(POPUP_TIMES);

                while (cur.moveToNext()) {
                    info = new PopupActionInfo();
                    info.sceneId = cur.getString(sceneIdIndex);
                    info.date = cur.getString(dateIndex);
                    info.parseTimes = cur.getInt(parseTimesIndex);
                    info.popupTimes = cur.getInt(popupTimesIndex);
                }
                return info;
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "queryInfoBySceneId: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return info;
    }

    public static long insertOrUpdateParse(HashMap<String, String> paramsMap) {
        String scene_id = paramsMap.get("titleNo");
        long result = -1;
        PopupActionInfo info = null;
        ContentValues values = null;
        try {

            if (!StringUtils.isNull(scene_id)) {
                info = queryInfoBySceneId(scene_id);
            }

            if (info != null) {

                info.parseTimes = info.parseTimes + 1;

                values = setContentValues(info);
                // 更新解析次数
                DBManager.update(TABLE_NAME, values, SCENE_ID + " = ? and "
                        + DATE + " = ? ", new String[] { info.sceneId,
                        info.date });

                return 0;
            } else {
                info = new PopupActionInfo();
                info.sceneId = scene_id;

                String date = DateUtils.getCurrentTimeString("yyyyMMdd");
                info.date = date;
                info.parseTimes = 1;
                // 插入一条记录
                values = setContentValues(info);
                result = DBManager.insert(TABLE_NAME, values);
            }
            DuoquUtils.getSdkDoAction().statisticAction(scene_id,
                    Constant.ACTION_PARSE, null);
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "insertOrUpdateParse: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
        }
        return result;
    }

    public static long insertOrUpdatePopup(HashMap<String, String> paramsMap) {
        String scene_id = paramsMap.get("titleNo");
        long result = -1;
        PopupActionInfo info = null;
        ContentValues values = null;

        try {

            if (!StringUtils.isNull(scene_id)) {
                info = queryInfoBySceneId(scene_id);
            }

            if (info != null) {

                info.popupTimes = info.popupTimes + 1;

                values = setContentValues(info);
                // 更新解析次数
                DBManager.update(TABLE_NAME, values, SCENE_ID + " = ? and "
                        + DATE + " = ? ", new String[] { info.sceneId,
                        info.date });

                return 0;
            } else {
                info = new PopupActionInfo();
                info.sceneId = scene_id;

                String date = DateUtils.getCurrentTimeString("yyyyMMdd");
                info.date = date;
                info.popupTimes = 1;
                // 插入一条记录
                values = setContentValues(info);
                result = DBManager.insert(TABLE_NAME, values);
            }

        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "insertOrUpdatePopup: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {

        }
        return result;
    }

    public static ContentValues setContentValues(PopupActionInfo info) {
        ContentValues values = new ContentValues();
        values.put(SCENE_ID, info.sceneId);
        values.put(DATE, info.date);
        values.put(PARSE_TIMES, info.parseTimes);
        values.put(POPUP_TIMES, info.popupTimes);
        return values;
    }

    public static void deleteAll() {
        try {
            DBManager.delete(TABLE_NAME, null, null);

        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "deleteAll: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
    }

    public static void deleteInfoByDate(String date) {
        try {
            DBManager.delete(TABLE_NAME, DATE + " < ?", new String[] { date });
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "deleteInfoByDate: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
    }

}
